<template>
    <div>
        <van-nav-bar  fixed=true  placeholder=true title="更新价格" left-text="返回" left-arrow @click-left="onClickLeft" />

        <van-cell-group>
            <van-field v-model="newPrice" type="number" label="租金¥/天：" placeholder="请输入租金" @input="priceChange" />
            <van-field v-model="newGuarantee" type="number" label="押金¥：" placeholder="请输入押金" @input="guarenteeChange" />
            <van-button size="large" round type="warning" @click="submit" v-bind:loading="showLoading"
            v-bind:disabled="disablesubmit" @submit.once="check">提交</van-button>
        </van-cell-group>
    </div>
</template>
<script>
import { Toast } from 'vant'
export default {
    name: "余额",
    data() {
        return {
            id: 0,
            newPrice: "",
            newGuarantee: "",
            disablesubmit: false,
            disablePrice: false,
            disableGurentee: false,
            showLoading: false,
        }
    },
    methods: {
        onClickLeft() {
            this.$router.go(-1)
        },
        submit() {
            this.showLoading = true
            this.axios({
                method: "patch",
                url: "/aapi/update_pro",
                data: {
                    id: this.id,
                    price: this.newPrice*1,
                    guarentee: this.newGuarantee*1
                }
            }).then(res => {
                console.log(res)
                this.showLoading = false
                this.$router.go(-1)
            }).error(() => {
                Toast.fail("网络错误请重试")
                this.showLoading = false
            })
        },
        priceChange(e) {
            if (e <= 0 || e == "") {
                this.disablePrice = true
                Toast.fail("价格不能小于0且不能为空")
            } else {
                this.disablePrice = false

            }
            this.disablesubmit = this.disableGurentee || this.disablePrice

        },
        guarenteeChange(e) {
            if (e < 0 || e == "") {
                this.disableGurentee = true
                Toast.fail("押金不能小于0且不能为空")
            } else {
                this.disableGurentee = false
            }
            this.disablesubmit = this.disableGurentee || this.disablePrice

        }
    },
    created() {
        this.id = this.$store.state.productId,
        this.newPrice = this.$store.state.updatePrice
        this.newGuarantee = this.$store.state.updateGuarentee
    }
}
</script>